<?php
	namespace app\index\Controller;

	use \think\Db;

	class Goods extends Base{
		public function index(){
			
			try{

				$id=(int)input('get.id');

				$row=[];

				if($id){
					$row=Db::name('goods')->field('g_id,g_name,g_title,g_icon,g_details,g_cateId,g_money,g_isSale,g_isSpec,g_limitNum,g_stock,g_dataFlag')->find($id);
				}

				if(!$row || $row['g_dataFlag']!=1){
					$this->json(['code'=>0,'msg'=>'该商品不存在或已下架']);
				}

				// $row['g_fold']=0;

				$disGoods=Db::name('discountgoods')->where(['d_goodsId'=>$id])->order('d_id desc')->find();
				// 查询该商品最后一次折扣数据
				
				$disInfo=[];

				if($disGoods){

					// 如果存在就把折扣信息查询出来，判断该折扣活动是否已经结束，以及状态是否开启

					$time=time();

					$disCount=Db::name('discount')->where(['d_status'=>1,'d_endTime'=>['>=',$time]])->find($disGoods['d_discountId']);

					if($disCount){

						$disInfo=['name'=>$disCount['d_name'],'fold'=>$disGoods['d_fold'],'startTime'=>$disCount['d_startTime'],'endTime'=>$disCount['d_endTime']];
					
					}

				}

				/*if($disCount){

					// 如果折扣活动还在进行中,就把折扣数量存入商品信息中

					$row['g_fold']=$disGoods['d_fold'];
				}*/

				$icons=Db::name('goodicon')->field('i_val')->where(['i_goodId'=>$id])->select();

				$attr=[];

				$tree=[];

				$list=[];

				$spec=true;

				if($row['g_isSpec']==1){

					/*
						如果该商品有规格选项，就把他对应的规格查询出来
					 */

					$attr=Db::name('goods_property')->where(['pro_goodsId'=>$id])->select();

					$attrId=Db::name('property')->where(['p_goodsId'=>$id])->select();


					$pNameid=explode(',',$attrId[0]['p_nameId']);
					// 获取该规格名称的id

					$pValid=join(array_column($attrId,'p_valId'),',');
					// 获取该规格值得ID 并把他们装换成字符
					
					$pValid=array_unique(explode(',',$pValid));
					// 切割字符去除重复的ID值

					$pname=[];


					$pval=[];


					$row2=Db::name('property_name')->where(['pname_id'=>['in',$pNameid]])->select();

					foreach($row2 as $k=>$v){
						$pname[$v['pname_id']]=$v;
					}

					$row3=Db::name('property_value')->where(['pval_id'=>['in',$pValid]])->select();

					foreach($row3 as $k=>$v){
						$pval[$v['pval_id']]=$v;
					}

					foreach($attr as $k=>$v){

						$s1=0;

						$s2=0;

						$s3=0;

						$arr=explode(',',$attrId[$k]['p_valId']);

						$n=0;

						foreach($arr as $k2=>$v2){
							if(isset($pval[$v2])){
								$n++;

								$str='s'.$n;

								$$str=$pval[$v2]['pval_id'];
							}
						}

						/*
						待处理BUG：如果有折扣活动，下面的金额未处理
						 */

						$money=$v['pro_price']*100;

						$list[]=['id'=>$v['pro_id'],'price'=>$v['pro_price']*100,'discount'=>$money*0.5,'s1'=>$s1,'s2'=>$s2,'s3'=>$s3,'stock_num'=>$v['pro_stock']];
					}

					$n=0;
					foreach($pNameid as $k=>$v){


						if(isset($pname[$v])){

							$n++;

							$data1=['k'=>$pname[$v]['pname_name'],'v'=>[],'k_s'=>'s'.$n];

							foreach($pval as $k2=>$v2){

								if($pname[$v]['pname_id']==$v2['pval_proId']){
									
									// unset($pval[$k2]);

									$data1['v'][]=['id'=>$v2['pval_id'],'name'=>$v2['pval_name']];



								}

							}

							$tree[]=$data1;


						}
					}

					$spec=false;
					
				}

				$message=['name'=>'订单留言','type'=>'text'];

				$sku=['tree'=>$tree,'list'=>$list,'price'=>$row['g_money'],'stock_num'=>$row['g_stock'],'none_sku'=>$spec,'message'=>$message,'hide_stock'=>false];

				$comments=Db::name('comments')->where(['com_goodsId'=>$id])->order('com_id desc')->select();

				$data=['goods'=>$row,'icons'=>$icons,'sku'=>$sku,'disInfo'=>$disInfo,'comments'=>$comments];

				return $this->data($data);

			}catch(\Exception $e){
				
				return $this->err($e);

			}

		}
	}
?>